package org.fhawk.tinyspring.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URL;

/**
 * Created by tuo on 8/11/2016.
 */
@SuppressWarnings("unchecked")
public class ClassUtil {
    private static final Logger logger = LoggerFactory.getLogger(ClassUtil.class);

    private ClassUtil() {
    }

    /**
     * 获取类加载器
     *
     * @return
     */
    public static ClassLoader getClassLoader() {
        return Thread.currentThread().getContextClassLoader();
    }

    /**
     * 获取classPath
     *
     * @return
     */
    public static String getClassPath() {
        String path = "";
        URL url = getClassLoader().getResource("");
        if (null != url) {
            path = url.getPath();
        }
        return path;
    }

    /**
     * 加载类  并自动初始化
     *
     * @param className
     * @return
     */
    public static Class<?> loadClass(String className) {
        return loadClass(className, true);
    }

    /**
     * 加载类
     *
     * @param className 类名
     * @param init      是否初始化
     * @return
     */
    public static Class<?> loadClass(String className, boolean init) {
        Class<?> cls = null;
        try {
            cls = Class.forName(className, init, getClassLoader());
        } catch (ClassNotFoundException e) {
            logger.error("load class error", e);
        }
        return cls;
    }



    /**
     * 原始类型转封装类
     * byte(字节)、short(短整型)、int(整数型)、long(长整型)、float(单精度浮点数类型)、double(双精度浮点数类型)、char(字符类型)、boolean(布尔类型)
     *
     * @param original
     * @return
     */
    private static String originalToPackage(String original) {
        String packageStr = null;
        switch (original) {
            case "byte":
                packageStr = "java.lang.Byte";
                break;
            case "short":
                packageStr = "java.lang.Short";
                break;
            case "int":
                packageStr = "java.lang.Integer";
                break;
            case "long":
                packageStr = "java.lang.Long";
                break;
            case "float":
                packageStr = "java.lang.Float";
                break;
            case "double":
                packageStr = "java.lang.Double";
                break;
            case "char":
                packageStr = "java.lang.Character";
                break;
            case "boolean":
                packageStr = "java.lang.Boolean";
                break;
            default:
                packageStr = original;
                break;
        }
        return packageStr;
    }

    /**
     * string 转换为其他类型(只处理八种基本类型)
     *
     * @param s
     * @param <T>
     * @return
     */
    public static <T> T castString(String s, Class<T> clazz) {
        T t;
        switch (clazz.getName()) {
            case "java.lang.Byte":
            case "byte":
                t = (T) Byte.valueOf(s);
                break;
            case "java.lang.Short":
            case "short":
                t = (T) Short.valueOf(s);
                break;
            case "java.lang.Integer":
            case "int":
                t = (T) Integer.valueOf(s);
                break;
            case "java.lang.Long":
            case "long":
                t = (T) Long.valueOf(s);
                break;
            case "java.lang.Float":
            case "float":
                t = (T) Float.valueOf(s);
                break;
            case "java.lang.Double":
            case "double":
                t = (T) Double.valueOf(s);
                break;
            case "java.lang.Boolean":
            case "boolean":
                t = (T) Boolean.valueOf(s);
                break;
            default:
                t = clazz.cast(s);
                break;
        }
        return t;
    }


    /**
     * 获得当前环境JDK版本
     *
     * @return
     */
    public static int getJDKVersion()
    {
        String version = System.getProperty("java.version");
        if (version != null && version.matches("1\\.\\d.*"))
        {
            int v = Integer.parseInt(version.charAt(2) + "");
            if (v >= 2)
            {
                return 44 + v;
            }
        }
        return -1;
    }

}
